<?php


namespace app\common\model;


use think\Model;
use traits\model\SoftDelete;

/**
 * 订单类
 * Class Order
 * @package app\common\model
 */
class Mtorder extends Model
{
    use SoftDelete;


    // 表名
    protected $name = 'mtofficial_order';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'datetime';
    protected $dateFormat = 'Y-m-d H:i:s';

    // 定义时间戳字段名
    protected $createTime = 'create_dt';
    protected $updateTime = 'update_dt';
    protected $deleteTime = 'delete_dt';

    // 追加属性
    protected $append = [
        "place_time_text",
        "pay_time_text",
        "expire_time_text",
        'order_status_text',
        'is_pay_text'
    ];



    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function shop()
    {
        return $this->belongsTo('app\admin\model\Shop', 'shop_no', 'shop_no');
    }

    public function play()
    {
        return $this->belongsTo('app\admin\model\Play', 'play_no', 'play_no');
    }

    public function play2()
    {
        return $this->belongsTo('app\admin\model\Play', 'play_no', 'play_no', [], 'LEFT')->setEagerlyType(0);
    }

    // public function proof()
    // {
    //     return $this->hasMany('app\common\model\OrderProof', 'play_order_no', 'play_order_no');
    // }

    public function getOrderStatusList($is_pay=0)
    {
        if ($is_pay==0) {
            return ['1' => '已报名，待下单','4'=>'已下单，待评价', '3' => '已完成', '-1' => '用户取消','-2'=>'超时取消','-4'=>'实付不足已取消','99'=>'审核中'];
        } else {
            return ['1' => '已报名，待下单','4'=>'已下单，待完成', '3' => '已完成', '-1' => '用户取消','-2'=>'超时取消','-4'=>'实付不足已取消','99'=>'审核中'];
        }
    }


    public function getIsPayList()
    {
        return ['1' => '已结算', '0'=>'未结算'];
    }

    public function getOrderStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_status']) ? $data['order_status'] : '');
        $is_pay=0;
        if (isset($data['is_pay']) && $data['is_pay']==1) $is_pay=1;
        $list = $this->getOrderStatusList($is_pay);
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getIsPayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_pay']) ? $data['is_pay'] : '');
        $list = $this->getIsPayList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getPlaceTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['place_time']) ? $data['place_time'] : '');
        return is_numeric($value) ? date("Y/m/d H:i", $value) : $value;
    }

    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y/m/d H:i", $value) : $value;
    }


    // public function getProofAttr($value, $data)
    // {
    //     return [];
    // }


    public function getexpireTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['expire_time']) ? $data['expire_time'] : '');
        return is_numeric($value) ? date("Y/m/d H:i", $value) : $value;
    }
}